<?php

    namespace app\listen\controller;

    class Message extends \app\common\controller\Base {
        private $messageModel;

        public static $MESSAGE_TYPE_SYSTEM = 9;
        public static $MESSAGE_TYPE_ZAN = 1;
        public static $MESSAGE_TYPE_LOVE = 2;
        public static $MESSAGE_TYPE_COMMENT = 3;
        public static $MESSAGE_TYPE_REPLY = 4;

        public function getMessageModel(): \app\listen\model\Message {
            if (empty($this->messageModel)) {
                $this->messageModel = model('listen/Message');
            }
            return $this->messageModel;
        }

        static function createModel($data) {
            if (empty($data)) {
                return ["error" => "no params", "data" => null];
            }
            $model = [];
            // 添加记录的需要变量
            $needKeys = ['sendUid' => 'send_uid', 'recipientUid' => 'recipient_uid', 'type' => 'type'];

            foreach ($needKeys as $_key => $sqlField) {
                if (!empty($data[$_key])) {
                    $model[$sqlField] = $data[$_key];
                } else {
                    return ["error" => $_key, "data" => null];
                }
            };

            $optionKeys = ['chainId' => 'chain_id'];
            foreach ($optionKeys as $_key => $sqlField) {
                if (!empty($data[$_key])) {
                    $model[$sqlField] = $data[$_key];
                }
            }
            $model['addtime'] = request()->time();
            return ["data" => $model];
        }
        // 移除消息
        public function del() {
            $data = $this->getParams();
            $id = $data['id'] ?? null;
            if (isset($id)) {
                $res = $this->getMessageModel()->where('id', $id)->delete();
            } else {
                $res = false;
            }
            if (false !== $res) {
                return api_return(10011, $res);
            } else {
                return api_return(-2017);
            }
        }
        public function add() {
            $data = $this->getParams();
            $model = self::createModel($data);

            if (!empty($model["error"])) {
                return api_return(-2017, $model["error"]);
            }

            $res = $this->getMessageModel()->add($model['data']);
            if (false !== $res) {
                return api_return(10011, $res);
            } else {
                return api_return(-2017);
            }
        }

        public function lists() {
            $data = $this->getParams();
            $recipientUid = $data['recipientUid'] ?? null;
            $sendUid = $data['sendUid'] ?? null;
            $type = $data['type'] ?? null;
            $page = $data['page'] ?? 1;
            $rows = $data['rows'] ?? 10;

            $info = $this->getMessageModel()->lists($recipientUid, $sendUid, $type, $page, $rows);
            if (false !== $info) {
                return api_return(10002, $info);
            } else {
                return api_return(-2017);
            }
        }
    }